package com.flycat.common.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author yalei.chen
 * @Date 2017/9/30
 * @Since JDK 1.8
 */
public class SessionUtils {

    //所有key都在这里定义
    public static final String LONIG_FLAG = "login_flag";    //登陆标识

    public static final String STORE_SESSION_USER = "user";  //用户
    public static final String STORE_SESSION_CODE = "code";  //验证码
    public static final String STORE_SESSION_PREURL = "preUrl"; //登录前输入url

    public static final String STORE_SESSION_CLIENT_USER = "clientUser";  //客户端用户

    //存放所有session中的key
    private static Set<String> sessionKey = new HashSet<String>();
    static {
        sessionKey.add(LONIG_FLAG);
        sessionKey.add(STORE_SESSION_USER);
        sessionKey.add(STORE_SESSION_CODE);
        sessionKey.add(STORE_SESSION_PREURL);
        sessionKey.add(STORE_SESSION_CLIENT_USER);
    }

    /**
     * 添加session值
     * @param request HttpServletRequest
     * @param key	  key
     * @param value   value
     */
    public static void setSession(HttpServletRequest request, String key, Object value) {
        if (null == request || StringUtils.isEmpty(key) || !validateKey(key)) {
            return;
        }
        request.getSession().setAttribute(key, value);
    }

    /**
     * 添加session值
     * @param session HttpSession
     * @param key	  key
     * @param value   value
     */
    public static void setSession(HttpSession session, String key, Object value) {
        if (null == session || StringUtils.isEmpty(key) || !validateKey(key)) {
            return;
        }
        session.setAttribute(key, value);
    }

    /**
     * 获取session值
     * @param session HttpSession
     * @param key     key
     * @return Object
     */
    public static Object getSession(HttpSession session, String key) {
        if (null == session || StringUtils.isEmpty(key) || !validateKey(key)) {
            return null;
        }
        return session.getAttribute(key);
    }

    /**
     * 获取session值
     * @param request HttpServletRequest
     * @param key     key
     * @return Object
     */
    public static Object getSession(HttpServletRequest request, String key) {
        if (null == request || StringUtils.isEmpty(key) || !validateKey(key)) {
            return null;
        }
        return request.getSession().getAttribute(key);
    }

    /**
     * 移除sesson
     * @param request HttpServletRequest
     * @param key     key
     *
     */
    public static void removeSession(HttpServletRequest request, String key) {
        if (null == request || StringUtils.isEmpty(key) || !validateKey(key)) {
            return ;
        }
        request.getSession().removeAttribute(key);
    }

    /**
     * 移除sesson
     * @param session HttpSession
     * @param key     key
     *
     */
    public static void removeSession(HttpSession session, String key) {
        if (null == session || StringUtils.isEmpty(key) || !validateKey(key)) {
            return ;
        }
        session.removeAttribute(key);
    }

    /**
     * 校验key是否合法
     * @param key
     * @return
     */
    private static boolean validateKey(String key) {
        return sessionKey.contains(key);
    }
}
